#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QDebug>

int main(int argc, char* argv[])
{
    QApplication a(argc, argv);
    //    MainWindow w;
    //    w.show();
    QFile file("file.dat");
    if (file.open(QIODevice::WriteOnly)) {
        QDataStream out(&file);
        out << (quint32)0xABCDEF00; // magic number, indentify the files
        out << (qint32)123; // file version
        //        out.setVersion(QDataStream::Qt_5_9); // software version
        QString str("This is the data!"); // saved data
        out << str;
        file.close();
    } else {
        qDebug("[WRITE]Open file error!");
    }
    if (file.open(QIODevice::ReadOnly)) {
        QDataStream in(&file);
        quint32 magicNum;
        in >> magicNum;
        if (magicNum != 0xABCDEF00) {
            qDebug("illegal file");
            return 1;
        }
        qint32 fileVer;
        in >> fileVer;
        if (fileVer < 100) {
            qDebug("file version is too old");
            return 2;
        } else if (fileVer > 123) {
            qDebug("file version is too new");
            return 3;
        }
        //        in.setVersion(QDataStream::Qt_5_9);
        QString str;
        in >> str;
        qDebug() << str;
        file.close();
    } else {
        qDebug("[READ]Open file error!");
    }
    return a.exec();
}
